package com.gxa.insurance.controller;

import com.gxa.insurance.exception.BusinessException;
import com.gxa.insurance.exception.ExceptionCode;
import com.gxa.insurance.pojo.City;
import com.gxa.insurance.service.CityService;
import com.gxa.insurance.util.JsonResult;
import com.gxa.insurance.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Min
 */
@RestController
@RequestMapping("/city")
@CrossOrigin
@Api("城市的接口")
public class CityController {
    @Autowired
    private CityService service;
    @GetMapping("/add")
    @ApiOperation(value = "添加城市的接口",produces = "query")
    public Map addCity(@RequestParam("city")String cityName) throws Exception {
        JsonResult jsonResult = new JsonResult();
        // 如果参数为空就抛出异常
        if(StringUtil.isEmpty(cityName)){
            throw new BusinessException(ExceptionCode.WRONG_VALUE);
        }
        Boolean isOk=service.addCity(cityName);
        if(isOk){
            jsonResult.setCode("1");
            jsonResult.setMsg("添加成功");
        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("添加失败");
        }
        return jsonResult.getMap();
    }

    @ApiOperation("查询所有城市名称")
    @GetMapping("/getName")
    public Map getAllNAme(){
        JsonResult jsonResult = new JsonResult();

        List<City> citys=service.selectAllName();

        if(citys.size()<=0){
            jsonResult.setCode("0");
            jsonResult.setMsg("列表为空");
        }else{
            jsonResult.setCode("1");
            jsonResult.setMsg("查询成功");
            jsonResult.setData(citys);
        }
        return jsonResult.getMap();
    }

    @ApiOperation("查询所有城市")
    @GetMapping("/getAllCity")
    public Map getAll(@RequestParam("page")Integer page,@RequestParam("pageSize")Integer pageSize){
        JsonResult jsonResult = new JsonResult();

        Map<String,Object> citys=service.selectAllCity(page,pageSize);
        if(citys.size()<=0){
            jsonResult.setCode("0");
            jsonResult.setMsg("列表为空");
        }else{
            jsonResult.setCode("1");
            jsonResult.setMsg("查询成功");
            jsonResult.setData(citys.get("list"));
            jsonResult.put("count",citys.get("count"));
        }
        return jsonResult.getMap();
    }

    @ApiOperation("根据id删除城市的接口")
    @GetMapping("/deleteCity")
    public Map deleteCityById(@RequestParam("id")Integer id){
        JsonResult jsonResult = new JsonResult();
        Boolean isOk=service.deleteCityById(id);
        if(isOk){
            jsonResult.setCode("1");
            jsonResult.setMsg("删除成功");
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("删除失败");
        }
        return jsonResult.getMap();
    }
}
